package com.lemon.smartsafety.ui.adapter.safe.inspection;

import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.event.inspection.InspectionCheckItemCountEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingItemCountVo;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/27.
 */
public class BuildingPositionForInspectionAdapter extends LemonRcvAdapter<CompanyBuildingItemCountVo> {
    private LoggerHelper loggerHelper = new LoggerHelper(BuildingPositionForInspectionAdapter.class);

    public BuildingPositionForInspectionAdapter(@Nullable List<CompanyBuildingItemCountVo> data) {
        super(data);
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingPositionForInspectionItem();
    }


    public class BuildingPositionForInspectionItem implements AdapterItem<CompanyBuildingItemCountVo> {

        LinearLayout   ripple;
        LinearLayout llMain;
        TextView     tvItemCount;
        TextView     tvPositionRemark;


        CompanyBuildingItemCountVo mData;
        int                        mPosition;

        public BuildingPositionForInspectionItem() {

        }

        @Override
        public int getLayoutResId() {
            return R.layout.item_position_inspection_view;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            llMain = (LinearLayout) root.findViewById(R.id.ll_main);
            tvItemCount = (TextView) root.findViewById(R.id.tv_item_count);
            tvPositionRemark = (TextView) root.findViewById(R.id.tv_position_remark);
        }

        @Override
        public void setViews() {
        }

        @Override
        public void handleData(final CompanyBuildingItemCountVo item, int position) {
            mPosition = position;
            mData = item;
            tvItemCount.setText(StringUtils.nullValue(item.getItemCount(), "0"));
            if (StringUtils.nullValue(mData.getTitleId()).length() != 0) {
                tvPositionRemark.setText(Html.fromHtml("<u>" + mData.getPositionTitle() + "(" + DataInfo.getChinesePosition(mData.getPositionMark()) + ")" + "</u>"));
            } else {
                tvPositionRemark.setText(Html.fromHtml("<u>" + DataInfo.getChinesePosition(mData.getPositionMark()) + "</u>"));
            }

            /**
             * 设置状态灯
             */
            if (DataInfo.RED_TAG.equals(StringUtils.nullValue(mData.getBadItemStatus()))) {
                //llMain.setBackgroundResource(R.color.colorAppRed);
                llMain.setBackgroundResource(R.drawable.base_appred_click_pressed);
            } else if (DataInfo.YELLOW_TAG.equals(StringUtils.nullValue(mData.getBadItemStatus()))) {
                //llMain.setBackgroundResource(R.color.colorAppYellow);
                llMain.setBackgroundResource(R.drawable.base_appyellow_click_pressed);
            } else {
                String itemCount = StringUtils.nullValue(item.getItemCount());
                if (!"0".equals(itemCount) && itemCount.length() != 0) {
                    //llMain.setBackgroundResource(R.color.colorAppGreen);
                    llMain.setBackgroundResource(R.drawable.base_appgreen_click_pressed);
                }
            }

            if (!"0".equals(StringUtils.nullValue(mData.getItemCount(), "0"))) {
                tvItemCount.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                ripple.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        InspectionCheckItemCountEvent event = new InspectionCheckItemCountEvent();
                        event.setData(item);
                        AppService.getBus().post(event);
                    }
                });
            } else {
                ripple.setEnabled(false);
            }
        }
    }
}
